package com.nowcoder.code.hjnc;

import com.algorithm.model.ListNode;
import com.algorithm.model.ListNodeService;

public class NC24 {

    public ListNode deleteDuplicates(ListNode head) {

        if (head == null || head.next == null) {
            return head;
        }

        ListNode pp = new ListNode(0);
        ListNode n = pp;
        pp.next = head;
        ListNode prev = head;
        head = head.next;

        while (head != null) {

            if (head.val == prev.val) {
                while (head != null && head.val == prev.val) {
                    head = head.next;
                }
                pp.next = head;
                prev = pp.next;
                if (head == null) {
                    break;
                }
                head = head.next;
            } else {
                pp = pp.next;
                prev = prev.next;
                head = head.next;
            }

        }

        return n.next;

    }

    public static void main(String[] args) {
        ListNode listNode = ListNodeService.getByArray(new int[]{1, 1, 2});

        ListNodeService.print(new NC24().deleteDuplicates(listNode));
    }

}
